package com.vivien.example.springbootdemo.controller;

import com.vivien.example.springbootdemo.service.EcordersService;
import com.vivien.example.springbootdemo.service.impl.EcordersServiceImpl;
import com.vivien.example.springbootdemo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/ecorders")
public class EcordersController {
    @Autowired
    private EcordersService orderService;

    @GetMapping("/category-price-sum")
    public Result getCategoryPriceSum(@RequestParam Integer userId) {
        Map<String, Double> result = orderService.getCategoryPriceSum(userId);
        Map<String, Object> response = new HashMap<>();
        response.put("userId", userId);
        List<Map<String, Object>> configuration = result.entrySet().stream()
                .map(entry -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", entry.getKey());
                    map.put("value", entry.getValue());
                    return map;
                })
                .collect(Collectors.toList());
        response.put("configuration", configuration);
        return new Result(200, "成功", response);
    }

    @GetMapping("/price-change-last5years")
    public Result getUserPriceChangeLast5Years(@RequestParam Integer userId) {
        Map<String, List<Double>> result = orderService.getUserPriceChangeLast5Years(userId);
        // 返回格式根据需求定义
        Map<String, Object> response = new HashMap<>();
        response.put("userId", userId);
        response.put("data", new HashMap<>(result));

        return new Result(200,"成功", response);
    }
}
